package com.qef.com.mmovie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVUser;
import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.base.BaseFragment;
import com.qef.com.mmovie.presenter.LoginPresenter;
import com.qef.com.mmovie.presenter.contract.LoginContract;
import com.qef.com.mmovie.utils.Constants;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by feq on 2017/4/9.
 */

public class LoginFragment extends BaseFragment<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_edit)
    TextView tvRegisture;
    private boolean isLogin = true;
    @BindView(R.id.email_sign_in_button)
    Button btLogin;
    @BindView(R.id.email)
    EditText mEdNmae;
    @BindView(R.id.password)
    EditText mEdPwd;
    private String mName;
    private String mPwd;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.loging_fragment;
    }

    @Override
    protected void initEventAndData() {
        initView(getView());
    }

    private void initView(View view) {
        initToolbarNav(toolbar);
    }

    public static LoginFragment newInstance() {
        Bundle bundle = new Bundle();
        LoginFragment loginFragment = new LoginFragment();
        loginFragment.setArguments(bundle);
        return loginFragment;
    }


    @Override
    public void showContent(AVUser avObject, int action) {
        if (action == Constants.LOGIN_ACTION) {
            Toast.makeText(App.getInstance(), "登录成功", Toast.LENGTH_SHORT).show();
            if (null == AVUser.getCurrentUser().get("headImg")) {
                //跳转到信息修改页面
                startActivity(new Intent().setClass(getActivity(), UpDataPerson.class));
            } else {
                _mActivity.onBackPressed();
            }
        } else {
            Toast.makeText(App.getInstance(), "注册成功", Toast.LENGTH_SHORT).show();
            mPresenter.login(mName, mPwd);
        }
        getActivity().finish();
    }

    @Override
    public void error(Throwable throwable, int action) {
        switch (action) {
            case Constants.LOGIN_ACTION:
                Toast.makeText(App.getInstance(), "登录失败", Toast.LENGTH_SHORT).show();
                break;
            case Constants.REGISTER_ACTION:
                Toast.makeText(App.getInstance(), "注册失败", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    protected void initToolbarNav(Toolbar toolbar) {

        toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(v -> _mActivity.onBackPressed());
//        toolbar.inflateMenu(R.menu.video_menu);
    }

    @OnClick({R.id.toolbar_edit, R.id.email_sign_in_button})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_edit:
                isLogin = !isLogin;
                if (isLogin) {
                    tvRegisture.setText("注册");
                    btLogin.setText("登录");
                } else {
                    tvRegisture.setText("登录");
                    btLogin.setText("注册");
                }
                break;
            case R.id.email_sign_in_button:
                mName = mEdNmae.getText().toString().trim();
                mPwd = mEdPwd.getText().toString().trim();
                if (isLogin) {
                    //进行登录操作
                    mPresenter.login(mName, mPwd);
                } else {
                    //进行注册操作
                    mPresenter.register(mName, mPwd);
                }
                break;
        }

    }
}
